package admin.user;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.CommonUtil;
import com.common.CreateNum;
import com.common.ResultData;
import com.filter.BaseController;
import com.intercepter.AuthPassport;

import beans.sys.UserBean;
import beans.sys.UserModel;
import beans.user.agentxh;
import beans.user.agentxhserach;
import beans.user.user;
import service.sys.UserServiceImpl;
import service.user.AgentXHServiceImpl;
import service.user.WXUserServiceImpl;
@Controller
@RequestMapping("/agentxh")
public class AgentXHController extends BaseController{
	@Resource
	private WXUserServiceImpl<user> wxUserServiceImpl;
	@Resource
	private AgentXHServiceImpl<agentxh> agentXHServiceImpl;

	@Resource
	private UserServiceImpl<UserBean> userServiceImpl;

	@RequestMapping("/index")
	public String index(String agentid) {
		getReq().setAttribute("agentid", agentid);
		return "admin/agentuser/xh/index";
	}
	
	@RequestMapping("/getdata")
	@ResponseBody
	public void DataGrid(agentxhserach model){
		ResultData<List<agentxh>> rd = new ResultData<>();
		try{
			rd.setPage(model.getPage());
			List<agentxh> m = agentXHServiceImpl.queryByList(model);
			
			rd.setRows(m);
			rd.setRecords(model.getPager().getRowCount());
			rd.setTotal(model.getPager().getPageCount());
		}
		catch(Exception e){
			e.printStackTrace();
		}
		responseJson(rd);
	}
	
	@RequestMapping("/createxh")
	public String createXh(String agentid) throws Exception {
		UserBean ub = userServiceImpl.queryById(agentid);
		getReq().setAttribute("ub", ub);
		return "admin/agentuser/xh/createxh";
	}

	@RequestMapping("/save")
	@ResponseBody
	public void save(String agentid,Integer num,Double money,Integer usecount,Integer usetime) {
		ResultData<String> rd = new ResultData<>();

		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
		String dateNowStr = sdf.format(d);
		List<agentxh> xhList = agentXHServiceImpl.queryTodaySerurity(dateNowStr);
		List<String> allSecurityList = new ArrayList<>();
		for(agentxh xh : xhList) {
			allSecurityList.add(xh.getNum());
		}
		List<agentxh> list = new ArrayList<>();
		Random r = new Random();
		UserBean u = (UserBean)getReq().getSession().getAttribute("user");
		for(int i=0;i<num;i++) {
			String bigNum = CreateNum.CreateSecurity(r, allSecurityList, dateNowStr);

			agentxh tem = new agentxh();
			tem.setId(UUID.randomUUID().toString());
			tem.setAddtime(d);
			tem.setAdduser(u.getId());
			tem.setAgentid(agentid);
			tem.setMoney(money);
			tem.setNum(bigNum);
			tem.setUsecount(usecount);
			tem.setUsetime(usetime);
			tem.setStatus("1");
			list.add(tem);
		}
		try {
			agentXHServiceImpl.adds(list);
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		rd.setType(1);
		rd.setMessage("生成成功!");
		responseJson(rd);
	}
	
	@RequestMapping("/agentxh")
	@AuthPassport(actionName="agentxh",controllerName="agentxh")
	public String agentxh() {
		return "admin/agentuser/xh/agentxh";
	}
	
	@RequestMapping("/agentgetdata")
	@ResponseBody
	@AuthPassport(actionName="agentxh",controllerName="agentxh")
	public void AgentDataGrid(agentxhserach model){
		ResultData<List<agentxh>> rd = new ResultData<>();
		try{
			UserBean u = (UserBean)getReq().getSession().getAttribute("user");
			model.setAgentid(u.getId());
			rd.setPage(model.getPage());
			List<agentxh> m = agentXHServiceImpl.queryByList(model);
			
			rd.setRows(m);
			rd.setRecords(model.getPager().getRowCount());
			rd.setTotal(model.getPager().getPageCount());
		}
		catch(Exception e){
			e.printStackTrace();
		}
		responseJson(rd);
	}
}
